<template>
  <div class="login-container">
    <el-form ref="form" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">系统登录</h3>
      </div>
      <el-form-item prop="username">
        <el-input
          ref="username"
          v-model="loginForm.username"
          :placeholder="'请输入用户名'"
          prefix-icon="el-icon-user"
          name="username"
          type="text"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          ref="password"
          v-model="loginForm.password"
          :placeholder="'请输入密码'"
          prefix-icon="el-icon-lock"
          name="password"
          type="password"
          tabindex="2"
          :show-password="true"
          @keyup.enter.native="loginIn"
        />
      </el-form-item>

      <el-button class="login-btn" size="mini" type="primary" :loading="loading" @click.native.prevent="loginIn">登录</el-button>

      <div class="tips-box">
        <div class="tips">
          <span>账号: amdin</span>
          <span>密码: amdin</span>
        </div>
        <div class="tips">
          <span>账号: editor</span>
          <span>密码: editor</span>
        </div>
        <el-button class="third-btn" size="mini" type="primary">第三方登录</el-button>
      </div>
    </el-form>
  </div>
</template>
<script>
import { validUserName } from '@/utils/validate'
export default {
  name: 'Login',
  data() {
    const nameValidator = (rule, value, callback) => {
      if (!validUserName(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validPassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请输入至少6位数!'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          {
            required: true, validator: nameValidator, trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            validator: validPassword
          }
        ]
      },
      loading: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect // 对应permission里面的redirct参数
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    getOtherQuery(query) {
      // 遍历 query取key值,然后使用reduce汇总
      Object.keys(query).reduce((acc, cur) => {
        if (acc !== 'redirect') {
          acc[cur] = query[acc]
        }
        return acc
      }, {})
      // reduce((当前值, 下一个值) => {}, 初始值)
      // 当没有传入初始值时，acc是从数组中第一个元素开始的，cur是第二个元素
      // 当传入初始值（initiaValue）后,第一个acc将是初始值(initivalValue)，cur将是数组中的第一个元素
    },
    loginIn() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          // 先进行登录获取 token
          this.$store.dispatch('user/login', this.loginForm).then((data) => {
            // 再进行页面进行跳转
            this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('err log')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .login-container .el-input input {
//     color: $cursor;
//   }
// }
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor
  }
}
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
  }
  input {
    background: transparent;
    border: 0px;
    border-radius: 0px;
    -webkit-appearance: none;
    padding: 12px 5px 12px 15px;
    color: $light_gray;
    caret-color: $cursor;
    height: 47px;

    &:-webkit-autofill {
      box-shadow: 0 0 0px 1000px $bg inset !important;
      -webkit-text-fill-color: $cursor !important;
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background-color: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: $cursor;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  height: 100%;
  background-color: $bg;
  overflow: hidden;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    margin: 0 auto;
    padding: 160px 35px 0;
    overflow: hidden;
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $light_gray;
      text-align: center;
      margin: 0px auto 40px auto;
      font-weight: bold;
    }
  }
  .login-btn {
    width: 100%;
    font-size: 14px;
  }
  .tips-box {
    position: relative;
    margin-top: 8px;
    height: 50px;
    .tips {
      span {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }
  }
  .third-btn {
    position: absolute;
    right: 0;
    bottom: 12px;
  }
}
</style>
